import os
import sh
from waflib import Logs

def build(bld):
    pdc2png_env = bld.all_envs['local'].derive()
    output = bld.path.get_bld().parent.parent.make_node('pdc2png')

    sources = ["../../src/fw/applib/vendor/uPNG/upng.c",
               "../../src/fw/applib/vendor/tinflate/tinflate.c",
               "../../src/fw/applib/graphics/8_bit/bitblt_private.c",
               "../../src/fw/applib/graphics/gpath.c",
               "../../src/fw/applib/graphics/gbitmap.c",
               "../../src/fw/applib/graphics/gbitmap_png.c",
               "../../src/fw/applib/graphics/gcolor_definitions.c",
               "../../src/fw/applib/graphics/gtypes.c",
               "../../src/fw/applib/graphics/graphics.c",
               "../../src/fw/applib/graphics/graphics_line.c",
               "../../src/fw/applib/graphics/graphics_circle.c",
               "../../src/fw/applib/graphics/graphics_private.c",
               "../../src/fw/applib/graphics/graphics_private_raw.c",
               "../../src/fw/applib/graphics/bitblt.c",
               "../../src/fw/applib/graphics/8_bit/framebuffer.c",
               "../../src/fw/applib/graphics/framebuffer.c",
               "../../src/fw/applib/graphics/gdraw_command.c",
               "../../src/fw/applib/graphics/gdraw_command_list.c",
               "../../src/fw/applib/graphics/gdraw_command_image.c",
               "../../src/fw/applib/graphics/gdraw_command_frame.c",
               "../../src/fw/applib/graphics/gdraw_command_sequence.c"]

    sources = [bld.path.find_node(s) for s in sources]
    sources.extend(bld.path.ant_glob('src/*.c'))

    includes = ["../../tests/stubs",
                "../../tests/fw/graphics",
                "../../src/fw/applib/vendor/uPNG",
                "../../src/fw/applib/vendor/tinflate",
                "../../tests/overrides/default/",
                "../../src/include",
                "../../src/fw",
                "../../src/libutil/includes",
                "../../src/fw/util/time",
                "../../applib-targets/overrides",
                "../../third_party/freertos",
                "../../third_party/freertos/FreeRTOS-Kernel/FreeRTOS/Source/include",
                "../../third_party/freertos/FreeRTOS-Kernel/FreeRTOS/Source/portable/GCC/ARM_CM3"]

    includes = [bld.path.find_node(i).abspath() for i in includes]

    # We are overriding the gcc toolchain include/time.h with our own
    pdc2png_env.CFLAGS.append('-D_TIME_H_')  # just to check/force our version of <time.h>
    pdc2png_env.CFLAGS.append('-I' + bld.path.abspath() + '/../src/fw/util/time')
    pdc2png_env.CFLAGS.append('-I' + bld.path.abspath() + '/src/include')

    pdc2png_env.CFLAGS.append('-Wno-gnu-variable-sized-type-not-at-end')

    pbi2png_script = bld.path.find_node('../pbi2png.py')
    pbi2png_copy = bld.path.get_bld().parent.parent.make_node('pbi2png.py')
    bld(rule="cp ${SRC} ${TGT}", source=pbi2png_script, target=pbi2png_copy)

    bld.program(source=sources,
                target=output,
                includes=includes,
                defines=['UNITTEST', 'SCREEN_COLOR_DEPTH_BITS=8',
                         # TODO: PBL-21014 Support arbitrary display sizes
                         'DISP_COLS=144', 'DISP_ROWS=168',
                         'DISPLAY_FRAMEBUFFER_BYTES=%d' % (144 * 168),
                         'PBL_COLOR', 'PBL_RECT', 'PLATFORM_SNOWY=1',
                         'PBI2PNG_EXE="../../tools/pbi2png.py"'],
                use=['libutil-local', 'libos_includes'],
                env=pdc2png_env)


# vim:filetype=python
